#include "topmenubar.h"
#include <QDateTime>

TopMenuBar::TopMenuBar(QWidget *parent) : QWidget(parent), loggedIn(false)
{
    // 创建按钮
    btnVideoMonitor = new QPushButton("视频监控", this);
    btnPlayback = new QPushButton("视频回放", this);
    btnElectronicMap = new QPushButton("电子地图", this);
    btnLogQuery = new QPushButton("日志查询", this);
    btnSystemSettings = new QPushButton("系统设置", this);
    btnLogin = new QPushButton("登录", this);
    
    // 创建时间标签
    timeLabel = new QLabel(this);
    timeLabel->setStyleSheet("font-weight: bold;");
    
    // 创建定时器
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &TopMenuBar::updateTime);
    timer->start(1000); // 每秒更新一次时间
    
    // 设置按钮样式
    QString buttonStyle = "QPushButton { background-color: #f0f0f0; border: 1px solid #cccccc; padding: 5px; }"
                         "QPushButton:hover { background-color: #e0e0e0; }"
                         "QPushButton:pressed { background-color: #d0d0d0; }";
    btnVideoMonitor->setStyleSheet(buttonStyle);
    btnPlayback->setStyleSheet(buttonStyle);
    btnElectronicMap->setStyleSheet(buttonStyle);
    btnLogQuery->setStyleSheet(buttonStyle);
    btnSystemSettings->setStyleSheet(buttonStyle);
    btnLogin->setStyleSheet(buttonStyle);
    
    // 设置按钮大小
    btnVideoMonitor->setMinimumHeight(30);
    btnPlayback->setMinimumHeight(30);
    btnElectronicMap->setMinimumHeight(30);
    btnLogQuery->setMinimumHeight(30);
    btnSystemSettings->setMinimumHeight(30);
    btnLogin->setMinimumHeight(30);
    
    // 创建水平布局
    layout = new QHBoxLayout(this);
    layout->setContentsMargins(10, 5, 10, 5);
    layout->setSpacing(10);
    
    // 添加按钮到布局
    layout->addWidget(btnVideoMonitor);
    layout->addWidget(btnPlayback);
    layout->addWidget(btnElectronicMap);
    layout->addWidget(btnLogQuery);
    layout->addWidget(btnSystemSettings);
    layout->addWidget(btnLogin);
    
    // 添加弹性空间使按钮靠左对齐，时间靠右对齐
    layout->addStretch();
    layout->addWidget(timeLabel);
    
    // 默认选中视频监控
    btnVideoMonitor->setStyleSheet(buttonStyle + "QPushButton { background-color: #d0d0d0; font-weight: bold; }");
    currentMode = "视频监控";
    
    // 初始化时间显示
    updateTime();
    
    // 连接按钮点击信号
    connect(btnVideoMonitor, &QPushButton::clicked, this, &TopMenuBar::onVideoMonitorClicked);
    connect(btnPlayback, &QPushButton::clicked, this, &TopMenuBar::onPlaybackClicked);
    connect(btnElectronicMap, &QPushButton::clicked, this, &TopMenuBar::onElectronicMapClicked);
    connect(btnLogQuery, &QPushButton::clicked, this, &TopMenuBar::onLogQueryClicked);
    connect(btnSystemSettings, &QPushButton::clicked, this, &TopMenuBar::onSystemSettingsClicked);
    connect(btnLogin, &QPushButton::clicked, this, &TopMenuBar::onLoginClicked);
}

TopMenuBar::~TopMenuBar()
{
}

QString TopMenuBar::getCurrentMode() const
{
    return currentMode;
}

void TopMenuBar::setCurrentMode(const QString &mode)
{
    // 设置当前模式
    // 需要更新按钮样式和currentMode变量
    currentMode = mode;
    
    // 设置按钮样式
    QString buttonStyle = "QPushButton { background-color: #f0f0f0; border: 1px solid #cccccc; padding: 5px; }"
                         "QPushButton:hover { background-color: #e0e0e0; }"
                         "QPushButton:pressed { background-color: #d0d0d0; }";
    QString activeButtonStyle = buttonStyle + "QPushButton { background-color: #d0d0d0; font-weight: bold; }";
    
    // 重置所有按钮样式
    btnVideoMonitor->setStyleSheet(buttonStyle);
    btnPlayback->setStyleSheet(buttonStyle);
    btnElectronicMap->setStyleSheet(buttonStyle);
    btnLogQuery->setStyleSheet(buttonStyle);
    btnSystemSettings->setStyleSheet(buttonStyle);
    
    // 根据模式设置对应按钮为激活状态
    if (mode == "视频监控") {
        btnVideoMonitor->setStyleSheet(activeButtonStyle);
    } else if (mode == "视频回放") {
        btnPlayback->setStyleSheet(activeButtonStyle);
    } else if (mode == "电子地图") {
        btnElectronicMap->setStyleSheet(activeButtonStyle);
    } else if (mode == "日志查询") {
        btnLogQuery->setStyleSheet(activeButtonStyle);
    } else if (mode == "系统设置") {
        btnSystemSettings->setStyleSheet(activeButtonStyle);
    }
}

void TopMenuBar::setCurrentTime(const QString &time)
{
    // 设置当前时间显示
    // 需要更新timeLabel的显示文本
    timeLabel->setText(time);
}

void TopMenuBar::setLoginStatus(bool loggedIn)
{
    // 设置登录状态
    // 需要更新按钮文本和loggedIn变量
    this->loggedIn = loggedIn;
    if (loggedIn) {
        btnLogin->setText("注销");
    } else {
        btnLogin->setText("登录");
    }
}

bool TopMenuBar::isLogged() const
{
    // 返回登录状态
    return loggedIn;
}

void TopMenuBar::onVideoMonitorClicked()
{
    // 视频监控按钮被点击
    // 需要更新按钮样式并发出modeChanged信号
    setCurrentMode("视频监控");
    emit modeChanged("视频监控");
}

void TopMenuBar::onPlaybackClicked()
{
    // 视频回放按钮被点击
    // 需要更新按钮样式并发出modeChanged信号
    setCurrentMode("视频回放");
    emit modeChanged("视频回放");
}

void TopMenuBar::onElectronicMapClicked()
{
    // 电子地图按钮被点击
    // 需要更新按钮样式并发出modeChanged信号
    setCurrentMode("电子地图");
    emit modeChanged("电子地图");
}

void TopMenuBar::onLogQueryClicked()
{
    // 日志查询按钮被点击
    // 需要更新按钮样式并发出modeChanged信号
    setCurrentMode("日志查询");
    emit modeChanged("日志查询");
}

void TopMenuBar::onSystemSettingsClicked()
{
    // 系统设置按钮被点击
    // 需要更新按钮样式并发出modeChanged信号
    setCurrentMode("系统设置");
    emit modeChanged("系统设置");
}

void TopMenuBar::onLoginClicked()
{
    // 登录按钮被点击
    // 需要根据当前登录状态决定是发出登录请求还是注销请求
    if (loggedIn) {
        // 当前已登录，发出注销请求信号
        emit logoutRequested();
    } else {
        // 当前未登录，发出登录请求信号
        emit loginRequested();
    }
}

void TopMenuBar::updateTime()
{
    // 获取当前时间并格式化
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString timeString = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
    
    // 更新时间标签显示
    timeLabel->setText(timeString);
}
